package gwtool.gui;

import gwtool.backend.DataSetController;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class UpdateGui extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7603408412820516299L;

	private final DataSetController controller;

	private JProgressBar progressBar;

	private JLabel lblStatus;

	public UpdateGui(DataSetController controller) {

		this.controller = controller;
		setSize(300, 200);
		initialize();
		performUpdate();
	}

	private void initialize() {

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 139, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE };
		getContentPane().setLayout(gridBagLayout);
		GridBagConstraints gbc_lblStatus = new GridBagConstraints();
		gbc_lblStatus.insets = new Insets(0, 0, 5, 0);
		gbc_lblStatus.gridx = 0;
		gbc_lblStatus.gridy = 2;
		getContentPane().add(getLblStatus(), gbc_lblStatus);
		GridBagConstraints gbc_progressBar = new GridBagConstraints();
		gbc_progressBar.insets = new Insets(0, 0, 5, 0);
		gbc_progressBar.gridx = 0;
		gbc_progressBar.gridy = 3;
		getContentPane().add(getProgressBar(), gbc_progressBar);
	}

	private void performUpdate() {

		// if (MainController.getInstance().getJFrame() != null) {
		// MainController.getInstance().getJFrame().setVisible(false);
		// }
		getProgressBar().setValue(1);
		getProgressBar().setIndeterminate(true);
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setLocationRelativeTo(null);
		setVisible(true);
		onlineUpdate();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
		}
		getProgressBar().setIndeterminate(false);
		setVisible(false);
		// if (MainController.getInstance().getJFrame() != null) {
		// MainController.getInstance().getJFrame().setVisible(true);
		// }
	}

	private void onlineUpdate() {

		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {

				controller.getComm().updateCache();
				SwingUtilities.invokeLater(new Runnable() {

					@Override
					public void run() {

						getProgressBar().setValue(30);
					}
				});
				controller.getComm().commitItems();
				SwingUtilities.invokeLater(new Runnable() {

					@Override
					public void run() {

						getProgressBar().setValue(60);
					}
				});
				controller.removeDataSetsWithChanges();
				controller.getComm().updateCache();
				SwingUtilities.invokeLater(new Runnable() {

					@Override
					public void run() {

						getProgressBar().setValue(100);
					}
				});
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
				}
				// controller.getComm().setFertig(true);
			}
		});
		t.start();
	}

	private JProgressBar getProgressBar() {

		if (progressBar == null) {
			progressBar = new JProgressBar(1, 100);
		}
		return progressBar;
	}

	private JLabel getLblStatus() {

		if (lblStatus == null) {
			lblStatus = new JLabel("Status");
		}
		return lblStatus;
	}
}
